
case $# in
  0) echo "Usage: $0 <rsh> <hostname> [-user user] [-access] command ..."
     exit 1
     ;;
esac

rsh=$1
shift
target=$1
shift
rcmd="$rsh $target"
case $DISPLAY in
  unix:*) DISPLAY=`echo $DISPLAY | sed 's/unix//'`
          ;;
esac
case $DISPLAY in
  :*) fullname=`hostname`
      DISPLAY=$fullname$DISPLAY
      ;;
esac

username=
xauth=
xxauth=
case x$XUSERFILESEARCHPATH in
  x) xpath='HOME=${HOME-`pwd`} '
     xxpath="HOME "
     ;;
  *) xpath='HOME=${HOME-`pwd`} XUSERFILESEARCHPATH=${XUSERFILESEARCHPATH-"'"$XUSERFILESEARCHPATH"'"} '
     xxpath="HOME XUSERFILESEARCHPATH "
     ;;
esac

command=
continue=:
while $continue; do
  case $1 in
    -user)   shift
             username="-l $1"
             rcmd="$rsh $target $username"
             shift
             case x$XAUTHORITY in
               x) XAUTHORITY="$HOME/.Xauthority"
                  ;;
             esac
             case x$XUSERFILESEARCHPATH in
               x) ;;
               *) xpath="XUSERFILESEARCHPATH=$XUSERFILESEARCHPATH "
                  xxpath="XUSERFILESEARCHPATH "
                  ;;
             esac
             ;;
    -access) shift
             xhost +$target
             ;;
    *)       continue=false
             ;;
  esac
done

case x$XAUTHORITY in
  x) ;;
  x*) xauth="XAUTHORITY=$XAUTHORITY "
      xxauth="XAUTHORITY "
      ;;
esac

vars="$xpath$xauth"DISPLAY="$DISPLAY"
exec $rcmd 'exec sh -c '"'$vars"'; export '"$xxpath$xxauth"' DISPLAY; exec '"$@'"
